/*
 * MainWindowController.java
 *
 * Created on November 7, 2006, 9:39 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.umn.cs5115.scheduler;

import edu.umn.cs5115.scheduler.calendar.Calendar;
import edu.umn.cs5115.scheduler.entities.Course;
import edu.umn.cs5115.scheduler.entities.CoursePart;
import edu.umn.cs5115.scheduler.entities.Section;
import edu.umn.cs5115.scheduler.framework.KeyValueAdapter;
import edu.umn.cs5115.scheduler.framework.KeyValueCoding;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver;
import edu.umn.cs5115.scheduler.framework.KeyValueObserver.SetMutationType;
import edu.umn.cs5115.scheduler.framework.gui.SelectionController;
import edu.umn.cs5115.scheduler.search.CourseInfo;
import java.util.Set;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import edu.umn.cs5115.scheduler.framework.DocumentController;
import edu.umn.cs5115.scheduler.framework.WindowController;

/**
 * Window controller for the main schedule window.  It is the responsibility of
 * the Window controller to create the main interface and bind it to the 
 * document.
 * @author grant
 */
public class MainWindowController extends WindowController
{
    /** Panel that will eventually hold the status bar. */
    private Composite statusBar;
    /** Panel containing the "quick add" interface and tree view of classes. */
    private LeftPanel leftPanel;
    /** Course information formatter the left panel. */
    private CourseInfo courseInfoFormatter;
    /** Actually displays the styled text produced by the CourseInfo formatter. */
    private StyledText courseInformation;
    /** Calendar view. */
    private Calendar calendar;
    
    /** Sash form grouping the left panel and the course information panel. */
    private SashForm leftSashForm;
    /** Sash form grouping the left sash form with the calendar. */
    private SashForm mainSashForm;
    
    /** Layout. */
    private GridLayout layout;
    
    /**
     * Creates a new instance of MainWindowController
     */
    public MainWindowController(final SchedulerDocument document)
    {
        super(document);
        
        Display display = DocumentController.getSharedDocumentController().getDisplay();
        Shell mainWindow = new Shell(display);
        
        layout = new GridLayout(1, true);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        mainWindow.setLayout(layout);
        
        mainSashForm = new SashForm(mainWindow, SWT.HORIZONTAL);
        mainSashForm.setLayout(new FillLayout());
        
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        mainSashForm.setLayoutData(data);
        
        statusBar = new Composite(mainWindow, 0)
        {
            public Point computeSize(int wHint, int hHint, boolean changed)
            {
                return new Point(wHint, 15);
            }
        };
        
        data = new GridData(SWT.FILL, SWT.TOP, true, false);
        data.minimumHeight = 15;
        statusBar.setLayoutData(data);
        
        leftSashForm = new SashForm(mainSashForm, SWT.VERTICAL);
        leftSashForm.setLayout(new FillLayout());
        leftPanel = new LeftPanel(leftSashForm, document);
	// create course info group
	Group courseInfo = new Group(leftSashForm, SWT.BORDER);
	courseInfo.setLayout(new FillLayout());
	courseInfo.setText("Course Info:");
        courseInformation = new StyledText(courseInfo, SWT.WRAP | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        courseInfoFormatter = new CourseInfo(courseInformation, null);
        document.getSelectionController().addListener(new KeyValueAdapter()
        {
            public void valueChanged(KeyValueCoding object, String key)
            {
                CoursePart coursePart = document.getSelectionController().getSelection();
                if (coursePart instanceof Section)
                    courseInfoFormatter.setText((Section)coursePart);
                else if (coursePart instanceof Course)
                    courseInfoFormatter.setText((Course)coursePart);
                else
                    courseInfoFormatter.setText("Select a course for more information.");
            }
        }, SelectionController.SELECTION_KEY);
        courseInfoFormatter.setText("Select a course for more information.");

        calendar = new Calendar(mainSashForm, document);
        
        mainSashForm.setWeights(new int[] {28, 72});
        leftSashForm.setWeights(new int[] {70, 30});
        
        mainWindow.setBounds(display.getClientArea());
        
        setWindow(mainWindow);
    }
}
